package com.young.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//@Configuration
public class TTLQueueConfig {
    //==========================================过期队列==============================================================
    /**
     * 以属性的方式定义ttl 队列
     * @return
     */
    @Bean
    public Queue TTLQueue(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 30000); // 队列中的消息未被消费则30秒后过期
        return new Queue("TTL_QUEUE", true, false, false, map);
    }

    /**
     * 过期队列交换机
     * @return
     */
    @Bean
    public DirectExchange TTLExchange() {
        return new DirectExchange("TTL_EXCHANGE", true, false);
    }

    /**
     * 将过期队列与交换机进行绑定
     * @return
     */
    @Bean
    public Binding bindingTTL() {
        return BindingBuilder.bind(TTLQueue()).to(TTLExchange()).with("TTL");
    }


}
